package ltu.thole;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/** Connects to a server socket. The user can send text-messages to server. */
public class Client extends Activity {
	/** Called when the activity is created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);
    }
	/** Called when the activity is destroyed. */
	@Override
    public void onDestroy() {
        super.onDestroy();
    }
    
	// Declaring a bunch of stuff...
    private Socket clientSocket;
    private PrintWriter out = null;
    //private BufferedReader in = null;
    private EditText serverTxt;
    private EditText portTxt;
    private EditText messageTxt;
    private TextView messages;
    
    /** Called when a button is clicked. */
    public void onClick(View view) {
    	
    	// Creating the already created 'views'. Stupid...
    	// Why can't I just write "serverTxt = R.id.editText_server" ?!?
    	// Well, I guess the Android development team got a good reason for this.
        serverTxt = (EditText)findViewById(R.id.editText_server);
        portTxt = (EditText)findViewById(R.id.editText_port);
        messageTxt = (EditText)findViewById(R.id.editText_sendMessage);
        messages = (TextView)findViewById(R.id.textView_messages);
        
    	// WHICH BUTTON WAS CLICKED?!? OMG!
    	switch (view.getId()) {
    	// Connect button is clicked.
    	case R.id.button_connect:
    		connect(serverTxt.getText().toString(), Integer.parseInt(portTxt.getText().toString()));
    		break;
    	// Disconnect button is clicked.
    	case R.id.button_disconnect:
    		try {
				clientSocket.close();
			} catch (IOException e) {
				sayLong(e.getMessage());
				e.printStackTrace();
			}
    		say("Disconnected!");
    		// Reset the layout to the orignal 'client' layout.
    		setContentView(R.layout.client);
    		break;
    	// Send button is clicked.
    	case R.id.button_sendMessage:
    		sendMessage();
    		say("Sending message...");
    		break;
    	default:
    		break;
    	}
    }
    
    /** Called when the user presses the send-button. */
    public void sendMessage() {
        try{
        	// Getting the text from the message-textfield.
        	String text = messageTxt.getText().toString();
        	// Send text message with the PrintWriter.
            out.println(text);
            // Update the message history textfield.
    		messages.setText(messages.getText() + "\n" + text);
    		// Reset the message-textfield.
    		messageTxt.setText("");
        } catch (Exception e){
        	sayLong(e.getMessage());
        }
    }
    
    /** Called when user presses the connect-button. Creates a Client socket. */
    public void connect(String server, int port) {
    	
    	try {
    		// Creating a client socket and connect to the specified server and port.
    		clientSocket = new Socket(server, port);
		    clientSocket.setSoTimeout(5000);
		    // Creating PrintWriter.
		    out = new PrintWriter(clientSocket.getOutputStream(), true);
		    // Not using this BufferedReader...
		    //in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		    // Changes the layout when connected to the socket-server.
		    setContentView(R.layout.client_connected);
		
    	} catch (UnknownHostException e) {
			sayLong(e.getMessage());
			e.printStackTrace();
			setContentView(R.layout.client);
		} catch (IOException e) {
			sayLong(e.getMessage());
			e.printStackTrace();
			setContentView(R.layout.client);
		}
    }
    
    /** Simple method for showing a text-message. */
    private void say(String text) {
    	Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    /** Simple method for showing a text-message (a bit longer). */
    private void sayLong(String text) {
    	Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
    
}
